import {
  login,
  logout,
  getInfo
} from '@/api/login'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
// import {
//   getUploadToken
// } from '../../api'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    user: {},
    brandId: 0,
    uploadToken: '',
    status: null
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_BRANDID: (state, brandId) => {
      state.brandId = brandId
    }
  },

  actions: {
    SaveToken({ commit }, token) {
      console.log('sssss', token.token)
      commit('SET_TOKEN', token.token)
      setToken(token.token)
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password, userInfo.role).then(res => {
          console.log(res)
          // const token = res.token_type + ' ' + res.access_token
          // setToken(token)
          // commit('SET_TOKEN', token)
          resolve(res)
        }).catch(error => {
          console.log('error in user' + error)
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      console.log(state)
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          // if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
          //   commit('SET_ROLES', data.roles)
          // } else {
          //   reject('getInfo: roles must be a non-null array !')
          // }
          // =========== for test ============
          // const roles = ['merchant']
          // const roles = ['designer']
          // const roles = ['admin']
          console.log(res, 77777)
          const roles = [res.role]
          res.roles = roles
          // =========== end =================
          commit('SET_ROLES', roles)
          commit('SET_STATUS', res.brand.status)
          commit('SET_USER', res)
          commit('SET_NAME', res.user.username)
          commit('SET_BRANDID', res.user.brandId)
          commit('SET_AVATAR', res.user.avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_USER', {})
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
